// pages/common/components/liveBox/liveBox.js
var app = getApp();
import time from '../../../../utils/time.js'

import {visiteImg} from '../../../../utils/imapi'

var app=getApp()

Component({
    properties: {
        liveList: {
            type:Array,
            default:[{waite_time:0,waite_time_str:'00:00:00'}],
            observer:function(newVal, oldVal){
                var that = this;
                var curTime = new Date()
                var timestamp = Date.parse(curTime) / 1000;
                newVal.forEach(item => {
                    if(item.live_status=='102'){
                        item.waite_time = item.start_time - timestamp
                        if(item.waite_time<0){
                            item.waite_time_str = '迟到了\n快开播吧'
                        } else {
                            item.waite_time_str = this.changeTime(item.start_time - timestamp) 
                        }
                    }
                    let start_time = time.getTimeStr(item.start_time * 1000)
                    let end_time = time.getTimeStr(item.end_time * 1000)
                    item.date = start_time + '至' + end_time
                })
                this.setData({
                    liveList: newVal
                },()=>{
                    that.downTime();
                })
            }
        },
        showTitle:Boolean,
        bgColor:String,
        is_select:{
            type: [Boolean, String],
            default: false
        },
		is_bigin:{
			type: Boolean,
			value: true
		},
    },
    data: {
        liveList: []
    },
    ready() {
        
    },
    methods: {
        addPro(e){//添加商品跳转页面
            var id = e.currentTarget.dataset.id;
            wx.navigateTo({
              url: '/pages/module/addProduct/addProduct?id=' + id,
            })
        },
        //商品详情
        liveDetail(e) {
            var id = e.currentTarget.dataset.id;
            var status = e.currentTarget.dataset.status;
            this.triggerEvent('detail',{id:id,status:status})
        },
        chooseLive(e){
            var index = e.currentTarget.dataset.index;
            this.triggerEvent('change', {index: e.currentTarget.dataset.index});
        },
        downTime(){
          var that = this;
          that.data.liveList.forEach(item => {
            if(item.live_status=='102'){
                item.waite_time -= 1
                if(item.waite_time<0){
                    item.waite_time_str = '迟到了\n快开播吧'
                } else {
                    item.waite_time_str = that.changeTime(item.waite_time)
                }
            }
          })
          that.setData({
            liveList:that.data.liveList
          })
          setTimeout(function(){
            that.downTime();
          },1000)
        },
        changeTime(remain_time){
            // 小时
            var hr = Math.floor(remain_time / 3600 % 24);
            if (hr < 10) {
                hr = "0" + hr;
            }
            // 分钟
            var min = Math.floor(remain_time / 60 % 60);
            if (min < 10) {
                min = "0" + min;
            }
            // 秒
            var sec = Math.floor(remain_time % 60);
            if (sec < 10) {
                sec = "0" + sec;
            }
            if(hr == '00'&&min=='00'&&sec=='00'){
                return 'end'
            } else {
                return (hr + ':' + min + ':' + sec)
            }
        },
        shareLive(e){
            
           console.log(e);
            var live_id = e.currentTarget.dataset.live_id

          //  var room_no = e.currentTarget.dataset.room_no

            // 根据 room_id , share_openid或者 share_uid 分享

         /*    var uid=app.globalData.userInfo.uid
            // 场景值 100020 直播间分享

            // share_mid_page

            // 中间页根据场景值来

            //直播间 分享图 share_img
            let share_img = e.currentTarget.dataset.share_img

            const link_url = visiteImg({
                bg: share_img,
                opt:3,
                uid:uid,
                path:'',
                scene:'100020,'+ uid+','+live_id // 场景,场景值 => 10006,10001  ; 场景 10006, 10001 用户 uid
              })


            const current = link_url  //获取当前点击的 图片 url
            var url=[];
            url.push(current);
            wx.previewImage({
                current:current,
                urls: url
            })
*/
            let customParams = encodeURIComponent(JSON.stringify({ path: 'pages/module/productDetail/productDetail' }))
                
            wx.navigateTo({
                url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${live_id}&custom_params=${customParams}`
            })



     


        }
    },
})
